package com.baijiaxing.weixin.family.controller;

import com.baijiaxing.weixin.family.common.aes.AesException;
import com.baijiaxing.weixin.family.common.aes.WXBizMsgCrypt;
import com.baijiaxing.weixin.family.common.consts.FamilyConsts;
import com.baijiaxing.weixin.family.client.service.FamilyMessageService;
import org.apache.commons.io.IOUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by June on 2016/12/17.
 */

@Controller
@RequestMapping("family")
public class FamilyMessageController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FamilyMessageService familyMessageService;

    @RequestMapping(value = "", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String signature(@RequestParam(value = "msg_signature", required = false) String msg_signature, //微信加密签名，msg_signature结合了企业填写的token、请求中的timestamp、nonce参数、加密的消息体
                            @RequestParam(value = "timestamp", required = false) String timestamp, //时间戳
                            @RequestParam(value = "nonce", required = false) String nonce, // 随机数
                            @RequestParam(value = "echostr", required = false) String echostr //加密的随机字符串，以msg_encrypt格式提供。需要解密并返回echostr明文，解密后有random、msg_len、msg、$CorpID四个字段，其中msg即为echostr明文
    ) {
        logger.info("[family-signature]接口注册 msg_signature:{},timestamp:{},nonce:{},echostr{}", msg_signature, timestamp, nonce, echostr);
        String echoStr = null;
        try {
            WXBizMsgCrypt wxcpt = FamilyConsts.getWXBizMsgCrypt();
            echoStr = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
        } catch (AesException e) {
            e.printStackTrace();
        }
        logger.info("解密后的echoStr：{}", echoStr);
        return echoStr;
    }

    @RequestMapping(value = "", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String reqMessage(HttpServletRequest request,
                             @RequestParam(value = "msg_signature", required = false) String msg_signature, //消息体签名
                             @RequestParam(value = "timestamp", required = false) String timestamp, //时间戳
                             @RequestParam(value = "nonce", required = false) String nonce //随机数字串
    ) {
        //加密信息
        String resMsgs = null;
        try {
            String sReqData = IOUtils.toString(request.getInputStream(), "utf-8");
            logger.info("[family-reqMessage]解密之前的数据 msg_signature:{},timestamp:{},nonce:{},sReqData{}", msg_signature, timestamp, nonce, sReqData);
            resMsgs = familyMessageService.responseDecryptMsg(msg_signature, timestamp, nonce, sReqData);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AesException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return resMsgs;
    }
}